TableCsvFactory.toCsv   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 14
rs 9.85
c 0
b 0
f 0
cc 1
1
import { Inject, Injectable } from '@nestjs/common';
2
import { ITranslator } from '../Translations/ITranslator';
3
import { Table } from './';
4
5
@Injectable()
6
export class TableCsvFactory {
7
  constructor(
8
    @Inject('ITranslator')
9
    private translator: ITranslator
10
  ) {}
11
12
  public toCsv(table: Table): string {
13
    const header = table.columns.map(column =>
14
      this.translator.translate(column.toString())
15
    );
16
    const rows = table.rows.map(row =>
17
      row
18
        .map(cell => {
19
          const text = cell.renderText();
20
          return `"${text}"`; // Use quotes to allow newlines
21
        })
22
        .join(';')
23
    );
24
    return [header.join(';'), ...rows].join('\n');
25
  }
26
}
27